﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using BaseDll;
namespace UserData
{
    public partial class Form_ScrewData : UIPage
    {
        public Form_ScrewData()
        {
            InitializeComponent();
        }
        public class ScrewData
        {
            public string strName = "";
            public int n_Index = 0;
            public double d_Torque = 0;
            public double d_Angle = 0;
            public double d_High = 0;
            public bool b_Result = true;
            public string strTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        string str_PathName = AppDomain.CurrentDomain.BaseDirectory + @"\Config.螺丝数据.json";
        private List<ScrewData> screwDatas = new List<ScrewData>();

        public void Function_AddScrewData(ScrewData data)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => Function_AddScrewData(data)));
            }
            else
            {
                screwDatas.Add(data);
                AddScrewData(data);
                AccessJosnSerializer.ObjectToJson(str_PathName, screwDatas);
            }
        }

        private void 清除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => 清除数据ToolStripMenuItem_Click(sender,e)));
            }
            else
            {
                screwDatas.Clear();
                dg_ScrewData.Rows.Clear();
                AccessJosnSerializer.ObjectToJson(str_PathName, screwDatas);
            }

        }

        private void Form_ScrewData_Load(object sender, EventArgs e)
        {
            if (!File.Exists(str_PathName))
            {
                AccessJosnSerializer.ObjectToJson(str_PathName, screwDatas);
            }
            else
            {
                object obj = AccessJosnSerializer.JsonToObject(str_PathName, typeof(List<ScrewData>));
                if (obj != null)
                {
                    screwDatas = (List<ScrewData>)obj;
                }
            }
            foreach (var item in screwDatas)
            {
                AddScrewData(item);
            }
        }

        private void AddScrewData(ScrewData data)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => AddScrewData(data)));
            }
            else
            {
                dg_ScrewData.Rows.Add(data.strName, data.n_Index, data.d_Torque, data.d_Angle, data.d_High, data.b_Result, data.strTime);
            }
        }

    }
}
